-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Call current user API v1/user
during sdk client initialization.
#33
Conversation
- add reference to client obj for all Resources - make client.resource initialization lazy
src/posit/connect/client.py
Outdated
if self._users is None: | ||
self._users = Users(client=self) | ||
return self._users |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we actually need (or want) to cache ._users? If I do a new query, I want to start fresh. And instantiating Users()
should be basically free.
if self._users is None: | |
self._users = Users(client=self) | |
return self._users | |
return Users(client=self) |
☂️ Python Coverage
Overall Coverage
New FilesNo new covered files... Modified Files
|
@property | ||
def users(self) -> CachedUsers: | ||
return Users(client=self) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to instantiate a new instance every time? The Users class is designed to fetch from the server once, and then use cached results for subsequent iterations.
There are pros and cons to fetching from the server every time.
We currently don't have any methods that mutate the server, but when we do, my thought is to invalidate the cache on methods that mutate (post, delete, put). This would trigger a fresh fetch on the next iteration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, passing self
to Users
instead of the session
and config
seems odd. This makes client.users.client.users.client.users
valid
If the goal is to reduce the boilerplate of passing two variables down to one, I would prefer creating a DTO object that wraps session
and config.
But, I still think that is overkill at this stage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Users class is designed to fetch from the server once, and then use cached results for subsequent iterations.
I think instantiating from Client
every time gives you the behavior you want. If you (the SDK user) want to use the cached results, do something like all_users = client.users.find()
and work with all_users
as much as you want. But some actions you'll want to do will modify the collection on the server, and you'll want a way to refresh. So then you go to client.users
again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, passing
self
toUsers
instead of thesession
andconfig
seems odd. This makesclient.users.client.users.client.users
valid
True, but IME it's useful to be able to get back to the original connection object from wherever you are in the API.
If the goal is to reduce the boilerplate of passing two variables down to one, I would prefer creating a DTO object that wraps
session
andconfig.
But, I still think that is overkill at this stage.
Isn't Client
already the object that wraps session
and config
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really have any strong opinion about how this happens. My only requirement at this stage is that there is some way to get information about the currently authenticated user from the v1/user
endpoint. I need this information to be available in the OAuthIntegration
Resource.
I'm fine with changing to approach to use DI instead if that's preferred. For example:
def OAuthIntegration(Resource):
def __init__(self, current_user: CurrentUser[Resource]):
...
def get_credentials():
guid = self.current_user.get().get('guid')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, but IME it's useful to be able to get back to the original connection object from wherever you are in the API.
session
holds the connection here.
Isn't Client already the object that wraps session and config?
Yes, but circular references should generally be avoided.
* doc: put some words in README.md (#38) * feat: adds passthrough methods for each HTTP request type (#25) * feat: adds connect_version property to Client (#39) * build: disables coverage threshold for new files (#41) * ci: adds main.yaml (#42) * ci: fixes main.yaml job name (#43) --------- Co-authored-by: Neal Richardson <neal.p.richardson@gmail.com>
I pulled the changes into #51, the conflicts with |
Note: I didn't fix the tests in
endpoints_test.py
because it looks like that code is replaced by the new Resource types.Should we remove
endpoints.py
?